package com.xenstra.bhavcopydownloader;

import static com.xenstra.bhavcopydownloader.Utils.convertToCalendar;

import java.io.File;
import java.util.Calendar;

/**
 * Main launcher of the the application.
 * Verifies the input data and starts the actual downloads.
 * @author sandip
 *
 */
public class BhavCopyDownloader {

	/**
	 * If args.length == 1 , only download folder is present. Download the current date's files.
	 * If args.length == 2 , download folder and download date is present. Download only that day's files.
	 * If args.length == 3 , download folder, start date and end date are present. Download the entire range of files.
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		if(args.length == 0){
			System.out.println("Usage : com.xenstra.bhavcopydownloader.BhavCopyDownloader <download folder> <<start date in dd/mmm/yyyy>> <<<end date in dd/mmm/yyyy>>>");
			System.exit(0);
		}
		
		// Check if download folder exists or not. MUST pre-exist.
		File downloadLocation = new File(args[0]);
		if(! downloadLocation.exists() || ! downloadLocation.isDirectory()){
			System.out.print("Download folder doesn't exist.");
			System.exit(0);
		}
		
		Calendar startDate = null, endDate = null;
		
		switch(args.length){
		case 1:	// only download folder is present. Download the current date's files.
			startDate = Calendar.getInstance();
			new Downloader(args[0], startDate).downloadSingleDayFiles();			
			break;
		case 2: // download folder and download date is present. Download only that day's files.
			startDate = convertToCalendar(args[1]);
			new Downloader(args[0], startDate).downloadSingleDayFiles();			
			break;			
		case 3:	// both start and end dates are present.
			// convert start date and end date to Calendar objects. End date must be greater or equal to start date.
			
			startDate = convertToCalendar(args[1]);
			endDate = convertToCalendar(args[2]);

			if (endDate.before(startDate)) {
				System.out.println("End date cannot be less than start date");
				System.exit(0);
			}
			
			new Downloader(args[0], startDate, endDate).startDownloads();			
			break;
		}						
	}



}
